package com.tanhuan.recommend.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.VoiceScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
@RocketMQMessageListener(topic = "tanhua-voice-151",consumerGroup = "tanhua-voice-151-group")
public class VoiceScoreListener implements RocketMQListener<String> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(String message) {
        //1. 转换 message = {"vid":"8","voiceId":"60a846a7d09026479c60b048","type":"1","userId":"1"}
        Map<String,String> map = JSON.parseObject(message, Map.class);
        //2. 获取消息数据
        Long userId = Long.parseLong(map.get("userId")+"");
        String voiceId = map.get("voiceId");
        Long vid = Long.parseLong(map.get("vid")+"");
        Integer type = Integer.parseInt(map.get("type")+"");

        //3. 创建评分对象
        VoiceScore vs = new VoiceScore();
        vs.setUserId(userId);
        vs.setVoiceId(vid);
        vs.setDate(System.currentTimeMillis());

        //4、根据不同的type，设置不同的评分
        switch (type) {
            case 1: { //语音
                vs.setScore(2d);
                break;
            }
            default: {
                vs.setScore(0d);
                break;
            }
        }


        // 保存操作评分到voice_score集合中
        mongoTemplate.save(vs);
    }
}
